package com.dianxin.smartpark.service.attendance.impl;

import cn.hutool.core.date.DateUtil;
import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.constant.ErrMsgEnum;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.dianxin.smartpark.dao.dataobject.attendance.*;
import com.dianxin.smartpark.dao.mapper.attendance.*;
import com.dianxin.smartpark.dao.qo.attendance.SmartParkAttendanceGroupQO;
import com.dianxin.smartpark.dao.qo.attendance.SmartParkHolidayDetailQO;
import com.dianxin.smartpark.service.attendance.SmartParkAttendanceGroupService;
import com.dianxin.smartpark.service.attendance.SmartParkHolidayDetailService;
import com.dianxin.smartpark.service.order.impl.SmartParkOrderRecordServiceImpl;
import com.dianxin.smartpark.service.utils.DateTimeUtil;
import com.dianxin.smartpark.service.vo.attendance.SmartParkAttendanceGroupVO;
import com.google.common.base.Joiner;
import com.sun.org.apache.bcel.internal.generic.ANEWARRAY;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @program: smart-park
 * @description: 考勤组管理
 * @author: chenyong
 * @create: 2021-07-29 18:12
 */
@Service
public class SmartParkAttendanceGroupServiceImpl implements SmartParkAttendanceGroupService {

    private final static Logger logger = LoggerFactory.getLogger(SmartParkAttendanceGroupServiceImpl.class);

    @Resource
    private SmartParkAttendanceGroupMapper smartParkAttendanceGroupMapper;
    @Resource
    private SmartParkGroupAccessControlMapper smartParkGroupAccessControlMapper;
    @Resource
    private SmartParkGroupAddressMapper smartParkGroupAddressMapper;
    @Resource
    private SmartParkGroupObjectMapper smartParkGroupObjectMapper;
    @Resource
    private SmartParkGroupSpecialDateMapper smartParkGroupSpecialDateMapper;
    @Resource
    private SmartParkGroupWeekdaysMapper smartParkGroupWeekdaysMapper;
    @Resource
    private SmartParkGroupWhiteListMapper smartParkGroupWhiteListMapper;
    @Resource
    private SmartParkAttendanceUserOccupyMapper smartParkAttendanceUserOccupyMapper;
    @Resource
    private SmartParkHolidayDetailService smartParkHolidayDetailService;
    @Resource
    private SmartParkShiftMapper smartParkShiftMapper;
    @Resource
    private SmartParkShiftDetailsMapper smartParkShiftDetailsMapper;
    @Resource
    private SmartParkAttendanceRecordMapper smartParkAttendanceRecordMapper;

    /**
     * @Description:  考勤组分页查询
     * @Author: ChenYong
     * @Date: 2021/7/30 0030
     * @param smartParkAttendanceGroupQO:
     * @Return: CommonResponseBody<List<SmartParkAttendanceGroupVO>>
     */
    @Override
    public CommonResponseBody<List<SmartParkAttendanceGroupVO>> getAttendanceGroups(SmartParkAttendanceGroupQO smartParkAttendanceGroupQO) {
        SmartParkAttendanceGroupExample recordExample = new SmartParkAttendanceGroupExample();
        SmartParkAttendanceGroupExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIsDeletedEqualTo(0);
        if(StringUtils.isBlank(smartParkAttendanceGroupQO.getTenantId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.EMPTY_PARAME.getErrorMessage(), ErrMsgEnum.EMPTY_PARAME.getErrorCode());
        }
        if (StringUtils.isNotBlank(smartParkAttendanceGroupQO.getTenantId())) {
            criteria.andTenantIdEqualTo(smartParkAttendanceGroupQO.getTenantId());
        }
        if (smartParkAttendanceGroupQO.getEnableStatus() != null) {
            criteria.andEnableStatusEqualTo(smartParkAttendanceGroupQO.getEnableStatus());
        }
        if (StringUtils.isNotBlank(smartParkAttendanceGroupQO.getAttendanceGroupName())) {
            criteria.andAttendanceGroupNameEqualTo(smartParkAttendanceGroupQO.getAttendanceGroupName());
        }
        if (smartParkAttendanceGroupQO.getNeedPage()) {
            recordExample.setOrderByClause("gmt_create desc limit " + (smartParkAttendanceGroupQO.getPageIndex() - 1) * smartParkAttendanceGroupQO.getPageSize() + "," + smartParkAttendanceGroupQO.getPageSize());
        } else {
            recordExample.setOrderByClause("gmt_create desc");
        }

        List<SmartParkAttendanceGroup> passRecords = smartParkAttendanceGroupMapper.selectByExample(recordExample);
        long count = smartParkAttendanceGroupMapper.countByExample(recordExample);

        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, smartParkAttendanceGroupQO, SmartParkAttendanceGroupVO.class);
    }

    /**
     * @Description:  保存考勤组信息
     * @Author: ChenYong
     * @Date: 2021/7/30 0030
     * @param smartParkAttendanceGroupQO:
     * @Return: CommonResponseBody
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResponseBody saveAttendanceGroup(SmartParkAttendanceGroupQO smartParkAttendanceGroupQO) {
        SmartParkAttendanceGroup smartParkAttendanceGroup = new SmartParkAttendanceGroup();

        try {
            BeanUtil.conversion(smartParkAttendanceGroupQO,smartParkAttendanceGroup);
            for (SmartParkGroupSpecialDate smartParkGroupSpecialDate : smartParkAttendanceGroupQO.getSpecialDates()){
                if(smartParkGroupSpecialDate.getShiftId() == null){
                    return ResponseBodyWrapperUtil.fail("请在特殊日期设置里选择班次","70001");
                }
            }
            for (SmartParkGroupWeekdays smartParkGroupWeekdays : smartParkAttendanceGroupQO.getWeekdaysList()){
                if(smartParkGroupWeekdays.getShiftId() == null){
                    return ResponseBodyWrapperUtil.fail("请在工作日设置里选择班次","70002");
                }
            }
            for (SmartParkGroupWhiteList whiteList : smartParkAttendanceGroupQO.getWhiteList()){
                SmartParkGroupWhiteListExample smartParkGroupWhiteListExample = new SmartParkGroupWhiteListExample();
                smartParkGroupWhiteListExample.or().andUserIdEqualTo(Long.valueOf(whiteList.getUserId()))
                        .andIsDeletedEqualTo(0);
                List<SmartParkGroupWhiteList> smartParkGroupWhiteLists = smartParkGroupWhiteListMapper.selectByExample(smartParkGroupWhiteListExample);
                if(smartParkGroupWhiteLists.size()>0){
                    return ResponseBodyWrapperUtil.fail("考勤对象："+whiteList.getUserName()+"已绑定其它考勤组，请重新绑定","70003");
                }
            }
            ArrayList<String> objectLists = new ArrayList<>();
            //保存考勤对象
            for (SmartParkGroupObject smartParkGroupObject : smartParkAttendanceGroupQO.getObjectList()){
                objectLists.add(String.valueOf(smartParkGroupObject.getUserId()));
            }
            ArrayList<String> whiteLists = new ArrayList<>();
            //保存白名单
            for (SmartParkGroupWhiteList smartParkGroupWhiteList : smartParkAttendanceGroupQO.getWhiteList()){
                whiteLists.add(String.valueOf(smartParkGroupWhiteList.getUserId()));
            }
            smartParkAttendanceGroup.setAttendanceObject(Joiner.on(",").join(objectLists));
            smartParkAttendanceGroup.setGroupWhiteList(Joiner.on(",").join(whiteLists));
            //保存班次和班次详情
            smartParkAttendanceGroup.beforeInsert();
            int insert = smartParkAttendanceGroupMapper.insert(smartParkAttendanceGroup);
            if(insert>0){
                //保存特殊日期
                for (SmartParkGroupSpecialDate smartParkGroupSpecialDate : smartParkAttendanceGroupQO.getSpecialDates()){
                    smartParkGroupSpecialDate.setGroupId(smartParkAttendanceGroup.getId());
                    smartParkGroupSpecialDate.setTenantId(smartParkAttendanceGroup.getTenantId());
                    smartParkGroupSpecialDate.setSpecialDate(DateTimeUtil.handle(smartParkGroupSpecialDate.getSpecialDate(),00,00));
                    smartParkGroupSpecialDate.beforeInsert();
                    smartParkGroupSpecialDateMapper.insert(smartParkGroupSpecialDate);
                }
                //保存工作日
                for (SmartParkGroupWeekdays smartParkGroupWeekdays : smartParkAttendanceGroupQO.getWeekdaysList()){
                    smartParkGroupWeekdays.setGroupId(smartParkAttendanceGroup.getId());
                    smartParkGroupWeekdays.setTenantId(smartParkAttendanceGroup.getTenantId());
                    smartParkGroupWeekdays.beforeInsert();
                    smartParkGroupWeekdaysMapper.insert(smartParkGroupWeekdays);
                }
                //保存门禁
                for (SmartParkGroupAccessControl smartParkGroupAccessControl : smartParkAttendanceGroupQO.getAccessControls()){
                    smartParkGroupAccessControl.setGroupId(smartParkAttendanceGroup.getId());
                    smartParkGroupAccessControl.setTenantId(smartParkAttendanceGroup.getTenantId());
                    smartParkGroupAccessControl.beforeInsert();
                    smartParkGroupAccessControlMapper.insert(smartParkGroupAccessControl);
                }
                //保存APP打卡地点
                for (SmartParkGroupAddress smartParkGroupAddress : smartParkAttendanceGroupQO.getAddressList()){
                    smartParkGroupAddress.setGroupId(smartParkAttendanceGroup.getId());
                    smartParkGroupAddress.setTenantId(smartParkAttendanceGroup.getTenantId());
                    smartParkGroupAddress.beforeInsert();
                    smartParkGroupAddressMapper.insert(smartParkGroupAddress);
                }
                //保存考勤对象
                for (SmartParkGroupObject object : smartParkAttendanceGroupQO.getObjectList()){
                    object.setGroupId(smartParkAttendanceGroup.getId());
                    object.setTenantId(smartParkAttendanceGroup.getTenantId());
                    object.beforeInsert();
                    smartParkGroupObjectMapper.insert(object);
                }
                //保存白名单
                for (SmartParkGroupWhiteList whiteList : smartParkAttendanceGroupQO.getWhiteList()){
                    whiteList.setGroupId(smartParkAttendanceGroup.getId());
                    whiteList.setTenantId(smartParkAttendanceGroup.getTenantId());
                    whiteList.beforeInsert();
                    smartParkGroupWhiteListMapper.insert(whiteList);
                }
            }
            return ResponseBodyWrapperUtil.toResData(insert);
        } catch (Exception e) {
            logger.error("",e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }
    }

    /**
     * @Description:  修改考勤组信息
     * @Author: ChenYong
     * @Date: 2021/7/30 0030
     * @param smartParkAttendanceGroupQO:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody updateAttendanceGroup(SmartParkAttendanceGroupQO smartParkAttendanceGroupQO) {
        SmartParkAttendanceGroup smartParkAttendanceGroup = new SmartParkAttendanceGroup();
        try {
            BeanUtil.conversion(smartParkAttendanceGroupQO,smartParkAttendanceGroup);
            for (SmartParkGroupSpecialDate smartParkGroupSpecialDate : smartParkAttendanceGroupQO.getSpecialDates()){
                if(smartParkGroupSpecialDate.getShiftId() == null){
                    return ResponseBodyWrapperUtil.fail("请在特殊日期设置里选择班次","70001");
                }
            }
            for (SmartParkGroupWeekdays smartParkGroupWeekdays : smartParkAttendanceGroupQO.getWeekdaysList()){
                if(smartParkGroupWeekdays.getShiftId() == null){
                    return ResponseBodyWrapperUtil.fail("请在工作日设置里选择班次","70002");
                }
            }

            ArrayList<String> objectLists = new ArrayList<>();
            //保存考勤对象
            for (SmartParkGroupObject smartParkGroupObject : smartParkAttendanceGroupQO.getObjectList()){
                objectLists.add(String.valueOf(smartParkGroupObject.getUserId()));
            }
            ArrayList<String> whiteLists = new ArrayList<>();
            //保存白名单
            for (SmartParkGroupWhiteList smartParkGroupWhiteList : smartParkAttendanceGroupQO.getWhiteList()){
                whiteLists.add(String.valueOf(smartParkGroupWhiteList.getUserId()));
            }
            smartParkAttendanceGroup.setAttendanceObject(Joiner.on(",").join(objectLists));
            smartParkAttendanceGroup.setGroupWhiteList(Joiner.on(",").join(whiteLists));
            smartParkAttendanceGroup.beforeUpdate();

            //保存班次和班次详情
            int update = smartParkAttendanceGroupMapper.updateByPrimaryKey(smartParkAttendanceGroup);

            if(update>0){
                //删除特殊日期
                smartParkGroupSpecialDateMapper.deleteGroupSpecialDate(smartParkAttendanceGroup.getId());
                //删除工作日
                smartParkGroupWeekdaysMapper.deleteGroupWeekdays(smartParkAttendanceGroup.getId());
                //删除门禁
                smartParkGroupAccessControlMapper.deleteGroupAccessControl(smartParkAttendanceGroup.getId());
                //删除APP打卡地点
                smartParkGroupAddressMapper.deleteGroupAddress(smartParkAttendanceGroup.getId());
                //删除白名单
                smartParkGroupObjectMapper.deleteGroupObject(smartParkAttendanceGroup.getId());
                //删除考勤对象
                smartParkGroupWhiteListMapper.deleteGroupWhiteList(smartParkAttendanceGroup.getId());

                for (SmartParkGroupWhiteList whiteList : smartParkAttendanceGroupQO.getWhiteList()){
                    SmartParkGroupWhiteListExample smartParkGroupWhiteListExample = new SmartParkGroupWhiteListExample();
                    smartParkGroupWhiteListExample.or().andUserIdEqualTo(Long.valueOf(whiteList.getUserId()))
                            .andIsDeletedEqualTo(0).andGroupIdNotEqualTo(smartParkAttendanceGroupQO.getId());
                    List<SmartParkGroupWhiteList> smartParkGroupWhiteLists = smartParkGroupWhiteListMapper.selectByExample(smartParkGroupWhiteListExample);
                    if(smartParkGroupWhiteLists.size()>0){
                        return ResponseBodyWrapperUtil.fail("考勤对象："+whiteList.getUserName()+"已绑定其它考勤组，请重新绑定","70003");
                    }
                }



                //保存特殊日期
                for (SmartParkGroupSpecialDate smartParkGroupSpecialDate : smartParkAttendanceGroupQO.getSpecialDates()){
                    smartParkGroupSpecialDate.setGroupId(smartParkAttendanceGroup.getId());
                    smartParkGroupSpecialDate.setTenantId(smartParkAttendanceGroup.getTenantId());
                    smartParkGroupSpecialDate.setSpecialDate(DateTimeUtil.handle(smartParkGroupSpecialDate.getSpecialDate(),00,00));
                    smartParkGroupSpecialDate.beforeInsert();
                    smartParkGroupSpecialDateMapper.insert(smartParkGroupSpecialDate);
                }
                //保存工作日
                for (SmartParkGroupWeekdays smartParkGroupWeekdays : smartParkAttendanceGroupQO.getWeekdaysList()){
                    smartParkGroupWeekdays.setGroupId(smartParkAttendanceGroup.getId());
                    smartParkGroupWeekdays.setTenantId(smartParkAttendanceGroup.getTenantId());
                    smartParkGroupWeekdays.beforeInsert();
                    smartParkGroupWeekdaysMapper.insert(smartParkGroupWeekdays);
                }
                //保存门禁
                for (SmartParkGroupAccessControl smartParkGroupAccessControl : smartParkAttendanceGroupQO.getAccessControls()){
                    smartParkGroupAccessControl.setGroupId(smartParkAttendanceGroup.getId());
                    smartParkGroupAccessControl.setTenantId(smartParkAttendanceGroup.getTenantId());
                    smartParkGroupAccessControl.beforeInsert();
                    smartParkGroupAccessControlMapper.insert(smartParkGroupAccessControl);
                }
                //保存APP打卡地点
                for (SmartParkGroupAddress smartParkGroupAddress : smartParkAttendanceGroupQO.getAddressList()){
                    smartParkGroupAddress.setGroupId(smartParkAttendanceGroup.getId());
                    smartParkGroupAddress.setTenantId(smartParkAttendanceGroup.getTenantId());
                    smartParkGroupAddress.beforeInsert();
                    smartParkGroupAddressMapper.insert(smartParkGroupAddress);
                }
                //保存考勤对象
                for (SmartParkGroupObject object : smartParkAttendanceGroupQO.getObjectList()){
                    object.setGroupId(smartParkAttendanceGroup.getId());
                    object.setTenantId(smartParkAttendanceGroup.getTenantId());
                    object.beforeInsert();
                    smartParkGroupObjectMapper.insert(object);
                }
                //保存白名单
                for (SmartParkGroupWhiteList whiteList : smartParkAttendanceGroupQO.getWhiteList()){
                    whiteList.setGroupId(smartParkAttendanceGroup.getId());
                    whiteList.setTenantId(smartParkAttendanceGroup.getTenantId());
                    whiteList.beforeInsert();
                    smartParkGroupWhiteListMapper.insert(whiteList);
                }
            }
            return ResponseBodyWrapperUtil.toResData(update);
        } catch (Exception e) {
            logger.error("",e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }
    }

    /**
     * @Description:  删除考勤组信息
     * @Author: ChenYong
     * @Date: 2021/7/30 0030
     * @param ids:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody deleteAttendanceGroup(Long[] ids) {
        int status = 0;
        for (Long id : ids){
            SmartParkAttendanceGroup smartParkAttendanceGroup = smartParkAttendanceGroupMapper.selectByPrimaryKey(id);
            smartParkAttendanceGroup.beforeDelete();
            //删除特殊日期
            smartParkGroupSpecialDateMapper.deleteGroupSpecialDate(smartParkAttendanceGroup.getId());
            //删除工作日
            smartParkGroupWeekdaysMapper.deleteGroupWeekdays(smartParkAttendanceGroup.getId());
            //删除门禁
            smartParkGroupAccessControlMapper.deleteGroupAccessControl(smartParkAttendanceGroup.getId());
            //删除APP打卡地点
            smartParkGroupAddressMapper.deleteGroupAddress(smartParkAttendanceGroup.getId());
            //删除考勤对象
            smartParkGroupObjectMapper.deleteGroupObject(smartParkAttendanceGroup.getId());
            //删除白名单
            smartParkGroupWhiteListMapper.deleteGroupWhiteList(smartParkAttendanceGroup.getId());
            status = smartParkAttendanceGroupMapper.updateByPrimaryKey(smartParkAttendanceGroup);
        }
        return ResponseBodyWrapperUtil.toResData(status);
    }

    /**
     * @Description:  查询详情
     * @Author: ChenYong
     * @Date: 2021/7/30 0030
     * @param smartParkAttendanceGroupQO:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody attendanceGroupDetails(SmartParkAttendanceGroupQO smartParkAttendanceGroupQO) {
        SmartParkAttendanceGroupVO smartParkAttendanceGroupVO = new SmartParkAttendanceGroupVO();
        try {
            SmartParkAttendanceGroup smartParkAttendanceGroup = smartParkAttendanceGroupMapper.selectByPrimaryKey(smartParkAttendanceGroupQO.getId());
            BeanUtil.conversion(smartParkAttendanceGroup,smartParkAttendanceGroupVO);

            //获取白名单信息
            SmartParkGroupWhiteListExample smartParkGroupWhiteListExample = new SmartParkGroupWhiteListExample();
            smartParkGroupWhiteListExample.or().andGroupIdEqualTo(smartParkAttendanceGroupVO.getId()).andIsDeletedEqualTo(0);
            List<SmartParkGroupWhiteList> smartParkGroupWhiteLists = smartParkGroupWhiteListMapper.selectByExample(smartParkGroupWhiteListExample);
            List<SmartParkGroupWhiteList> whiteListLongList = new ArrayList<>();
            smartParkAttendanceGroupVO.setWhiteList(smartParkGroupWhiteLists);

            //获取考勤对象信息
            SmartParkGroupObjectExample smartParkGroupObjectExample = new SmartParkGroupObjectExample();
            smartParkGroupObjectExample.or().andGroupIdEqualTo(smartParkAttendanceGroupVO.getId()).andIsDeletedEqualTo(0);
            List<SmartParkGroupObject> smartParkGroupObjects = smartParkGroupObjectMapper.selectByExample(smartParkGroupObjectExample);
            List<String> groupObjectLongList = new ArrayList<>();
            smartParkAttendanceGroupVO.setObjectList(smartParkGroupObjects);

            //获取考门禁设备信息
            SmartParkGroupAccessControlExample smartParkGroupAccessControlExample = new SmartParkGroupAccessControlExample();
            smartParkGroupAccessControlExample.or().andGroupIdEqualTo(smartParkAttendanceGroupVO.getId()).andIsDeletedEqualTo(0);
            List<SmartParkGroupAccessControl> smartParkGroupAccessControls = smartParkGroupAccessControlMapper.selectByExample(smartParkGroupAccessControlExample);
            smartParkAttendanceGroupVO.setAccessControls(smartParkGroupAccessControls);

            //打卡地点
            SmartParkGroupAddressExample smartParkGroupAddressExample = new SmartParkGroupAddressExample();
            smartParkGroupAddressExample.or().andGroupIdEqualTo(smartParkAttendanceGroupVO.getId()).andIsDeletedEqualTo(0);
            List<SmartParkGroupAddress> smartParkGroupAddresses = smartParkGroupAddressMapper.selectByExample(smartParkGroupAddressExample);
            smartParkAttendanceGroupVO.setAddressList(smartParkGroupAddresses);

            //获取特殊日期信息
            SmartParkGroupSpecialDateExample smartParkGroupSpecialDateExample = new SmartParkGroupSpecialDateExample();
            smartParkGroupSpecialDateExample.or().andGroupIdEqualTo(smartParkAttendanceGroupVO.getId()).andIsDeletedEqualTo(0);
            List<SmartParkGroupSpecialDate> smartParkGroupSpecialDates = smartParkGroupSpecialDateMapper.selectByExample(smartParkGroupSpecialDateExample);
            smartParkAttendanceGroupVO.setSpecialDates(smartParkGroupSpecialDates);

            //获取工作日信息
            SmartParkGroupWeekdaysExample smartParkGroupWeekdaysExample = new SmartParkGroupWeekdaysExample();
            smartParkGroupWeekdaysExample.or().andGroupIdEqualTo(smartParkAttendanceGroupVO.getId()).andIsDeletedEqualTo(0);
            List<SmartParkGroupWeekdays> smartParkGroupWeekdays = smartParkGroupWeekdaysMapper.selectByExample(smartParkGroupWeekdaysExample);
            smartParkAttendanceGroupVO.setWeekdaysList(smartParkGroupWeekdays);


            return ResponseBodyWrapperUtil.successWrapper(smartParkAttendanceGroupVO);
        } catch (Exception e) {
            logger.error("",e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }

    }

    /**
     * 通过userId获取绑定的考勤组
     * @Description:  通过userId获取绑定的考勤组
     * @Author: ChenYong
     * @Date: 2021/9/2 0002
     * @param smartParkAttendanceGroupQO:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody getAttendanceGroupByUserId(SmartParkAttendanceGroupQO smartParkAttendanceGroupQO) {

        Date date = new Date();
        if(smartParkAttendanceGroupQO.getDate()!=null){
            date = smartParkAttendanceGroupQO.getDate();
        }
        String format = "yyyy-MM-dd HH:mm:ss";
        String clockInDateFormat = "yyyy-MM-dd";
        //打卡日期
        String clockInDate = DateUtil.format(date, clockInDateFormat);
        try {
            SmartParkAttendanceGroupVO smartParkAttendanceGroupVO = new SmartParkAttendanceGroupVO();

            SmartParkGroupObjectExample smartParkGroupObjectExample = new SmartParkGroupObjectExample();
            smartParkGroupObjectExample.or().andUserIdEqualTo(smartParkAttendanceGroupQO.getUserId()).andIsDeletedEqualTo(0);
            List<SmartParkGroupObject> smartParkGroupObjects = smartParkGroupObjectMapper.selectByExample(smartParkGroupObjectExample);
            if(smartParkGroupObjects.size()>0){
                SmartParkAttendanceGroup smartParkAttendanceGroup = smartParkAttendanceGroupMapper.selectByPrimaryKey(smartParkGroupObjects.get(0).getGroupId());
                if(smartParkAttendanceGroup!=null){
                    BeanUtil.conversion(smartParkAttendanceGroup,smartParkAttendanceGroupVO);

                    //判断是否有占用信息
                    SmartParkAttendanceUserOccupy smartParkAttendanceUserOccupy = new SmartParkAttendanceUserOccupy();
                    smartParkAttendanceUserOccupy.setNowDate(date);
                    smartParkAttendanceUserOccupy.setUserId(smartParkAttendanceGroupQO.getUserId());
                    List<SmartParkAttendanceUserOccupy> smartParkAttendanceUserOccupies = smartParkAttendanceUserOccupyMapper.selectByTime(smartParkAttendanceUserOccupy);
                    if (smartParkAttendanceUserOccupies.size() > 0) {
                        return ResponseBodyWrapperUtil.fail("今日" + smartParkAttendanceUserOccupies.get(0).getBusinessType() + "中", "80021");
                    }

                    List<SmartParkGroupWhiteList> smartParkGroupWhiteLists = new ArrayList<>();
                    if (smartParkAttendanceGroupQO.getUserId() != null) {
                        SmartParkGroupWhiteListExample smartParkGroupWhiteListExample = new SmartParkGroupWhiteListExample();
                        smartParkGroupWhiteListExample.or().andUserIdEqualTo(smartParkAttendanceGroupQO.getUserId())
                                .andTenantIdEqualTo(smartParkAttendanceGroupQO.getTenantId())
                                .andIsDeletedEqualTo(0);
                        ;
                        smartParkGroupWhiteLists = smartParkGroupWhiteListMapper.selectByExample(smartParkGroupWhiteListExample);
                    }
                    if (smartParkGroupWhiteLists.size() > 0) {
                        return ResponseBodyWrapperUtil.fail("无需打卡", "80001");
                    }

                    if (smartParkAttendanceGroup.getEnableStatus() == 0) {
                        return ResponseBodyWrapperUtil.fail("考勤组未启用", "80010");
                    }

                    if (smartParkAttendanceGroup.getAppClockInEnableStatus() == 0) {
                        return ResponseBodyWrapperUtil.fail("APP打卡未启用", "80012");
                    }

                    //判断当天是否为特殊日期
                    SmartParkGroupSpecialDateExample smartParkGroupSpecialDateExample = new SmartParkGroupSpecialDateExample();
                    Date handle = DateTimeUtil.handle(date, 00, 00);
                    smartParkGroupSpecialDateExample.or().andSpecialDateEqualTo(handle)
                            .andGroupIdEqualTo(smartParkAttendanceGroup.getId())
                            .andTenantIdEqualTo(smartParkAttendanceGroupQO.getTenantId())
                            .andIsDeletedEqualTo(0);
                    List<SmartParkGroupSpecialDate> smartParkGroupSpecialDates = smartParkGroupSpecialDateMapper.selectByExample(smartParkGroupSpecialDateExample);
                    Long shiftId = null;
                    //打卡时间的工作日
                    int weekDay = 0;
                    if (smartParkGroupSpecialDates.size() > 0) {
                        //当前日期为特殊日期
                        //如果是判断是否需要上班
                        if (smartParkGroupSpecialDates.get(0).getShiftId() != 0) {
                            shiftId = smartParkGroupSpecialDates.get(0).getShiftId();
                        } else {
                            return ResponseBodyWrapperUtil.fail("当前日期为休息日", "80004");
                        }
                    }else {
                        //当前日期不是特殊日期
                        //获取节假日信息
                        String times = DateUtil.format(date, format);
                        SmartParkHolidayDetailQO smartParkHolidayDetailQO = new SmartParkHolidayDetailQO();
                        smartParkHolidayDetailQO.setTimes(times);
                        smartParkHolidayDetailQO.setTenantId(smartParkAttendanceGroupQO.getTenantId());
                        CommonResponseBody commonResponseBody = smartParkHolidayDetailService.queryByDate(smartParkHolidayDetailQO);
                        //判断是否需要打卡
                        if ((Boolean) commonResponseBody.getData()) {
                            //需要打卡
                            //获取工作日班次信息
                            weekDay = DateTimeUtil.getWeekDay(date);
                            SmartParkGroupWeekdaysExample smartParkGroupWeekdaysExample = new SmartParkGroupWeekdaysExample();
                            smartParkGroupWeekdaysExample.or().andTenantIdEqualTo(smartParkAttendanceGroupQO.getTenantId())
                                    .andGroupIdEqualTo(smartParkAttendanceGroup.getId())
                                    .andWeekDayNumEqualTo(weekDay)
                                    .andIsDeletedEqualTo(0);
                            List<SmartParkGroupWeekdays> smartParkGroupWeekdays = smartParkGroupWeekdaysMapper.selectByExample(smartParkGroupWeekdaysExample);
                            if (smartParkGroupWeekdays.size() < 1) {
                                return ResponseBodyWrapperUtil.fail("未获取到对应的工作日信息", "80005");
                            }
                            shiftId = smartParkGroupWeekdays.get(0).getShiftId();
                        } else {
                            return ResponseBodyWrapperUtil.fail("当前日期为休息日", "80004");
                        }
                    }

                    SmartParkShift smartParkShift = null;
                    if (shiftId != null && shiftId != 0) {
                        smartParkShift = smartParkShiftMapper.selectByPrimaryKey(shiftId);

                        if (smartParkShift == null) {
                            return ResponseBodyWrapperUtil.fail("未获取到班次信息", "80007");
                        }
                        if (smartParkShift.getEnableStatus() == 0) {
                            return ResponseBodyWrapperUtil.fail("当前班次未启用", "80009");
                        }
                        SmartParkShiftDetailsExample smartParkShiftDetailsExample = new SmartParkShiftDetailsExample();
                        smartParkShiftDetailsExample.or().andShiftIdEqualTo(smartParkShift.getId()).andIsDeletedEqualTo(0);
                        List<SmartParkShiftDetails> smartParkShiftDetailsList = smartParkShiftDetailsMapper.selectByExample(smartParkShiftDetailsExample);
                        List<SmartParkShiftDetails> list = new ArrayList<>();
                        for (SmartParkShiftDetails smartParkShiftDetails : smartParkShiftDetailsList){
                            //获取当前打卡时间段的上班打卡记录
                            SmartParkAttendanceRecordExample SignInSmartParkAttendanceRecordExample = new SmartParkAttendanceRecordExample();
                            SignInSmartParkAttendanceRecordExample.or().andUserIdEqualTo(smartParkAttendanceGroupQO.getUserId())
                                    .andDeptIdEqualTo(smartParkAttendanceGroupQO.getDeptId())
                                    .andCardShiftEqualTo(smartParkShift.getId())
                                    .andGroupIdEqualTo(smartParkAttendanceGroup.getId())
                                    .andShiftWorkTimeEqualTo(smartParkShiftDetails.getSignInTime())
                                    .andClockInDateEqualTo(clockInDate)
                                    .andIsDeletedEqualTo(0);
                            List<SmartParkAttendanceRecord> SignInSmartParkAttendanceRecords = smartParkAttendanceRecordMapper.selectByExample(SignInSmartParkAttendanceRecordExample);
                           if(SignInSmartParkAttendanceRecords.size()>0){
                               smartParkShiftDetails.setSignInRecord(SignInSmartParkAttendanceRecords.get(0));
                           }

                            //获取当前打卡时间段的下班打卡记录
                            SmartParkAttendanceRecordExample SignOutSmartParkAttendanceRecordExample = new SmartParkAttendanceRecordExample();
                            SignOutSmartParkAttendanceRecordExample.or().andUserIdEqualTo(smartParkAttendanceGroupQO.getUserId())
                                    .andDeptIdEqualTo(smartParkAttendanceGroupQO.getDeptId())
                                    .andCardShiftEqualTo(smartParkShift.getId())
                                    .andGroupIdEqualTo(smartParkAttendanceGroup.getId())
                                    .andShiftWorkTimeEqualTo(smartParkShiftDetails.getSignOutTime())
                                    .andClockInDateEqualTo(clockInDate)
                                    .andIsDeletedEqualTo(0);
                            List<SmartParkAttendanceRecord> SignOutSmartParkAttendanceRecords = smartParkAttendanceRecordMapper.selectByExample(SignOutSmartParkAttendanceRecordExample);
                            if(SignOutSmartParkAttendanceRecords.size()>0){
                                smartParkShiftDetails.setSignOutRecord(SignOutSmartParkAttendanceRecords.get(0));
                            }
                            list.add(smartParkShiftDetails);
                        }
                        smartParkShift.setDetailVOList(smartParkShiftDetailsList);

                        //打卡地点
                        SmartParkGroupAddressExample smartParkGroupAddressExample = new SmartParkGroupAddressExample();
                        smartParkGroupAddressExample.or().andGroupIdEqualTo(smartParkAttendanceGroupVO.getId()).andIsDeletedEqualTo(0);
                        List<SmartParkGroupAddress> smartParkGroupAddresses = smartParkGroupAddressMapper.selectByExample(smartParkGroupAddressExample);

                        smartParkAttendanceGroupVO.setAddressList(smartParkGroupAddresses);

                        smartParkAttendanceGroupVO.setSmartParkShift(smartParkShift);
                    } else {
                        return ResponseBodyWrapperUtil.fail("当前日期为休息日或班次ID为空", "80008");
                    }
                }
            }
            CommonResponseBody commonResponseBody = new CommonResponseBody();
            commonResponseBody.setData(smartParkAttendanceGroupVO);
            commonResponseBody.setErrMsg("请求成功");
            commonResponseBody.setErrCode("200");
            commonResponseBody.setSuccess(true);
            return commonResponseBody;
        } catch (Exception e) {
            logger.error("",e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }
    }
}
